package com.jimall.tdd;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * Tick Tac Toe main class
 *
 * @author jimall
 */
@Data
public class TickTacToe {
    /**
     * record position
     */
    @Getter(AccessLevel.PRIVATE)
    @Setter(AccessLevel.PRIVATE)
    private char[][] board;

    /**
     * record who to play
     */
    private boolean isX;

    public TickTacToe() {
        board = new char[3][3];

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                board[i][j] = ' ';
            }
        }

        isX = true;
    }

    public void print() {
        for (char[] chars : board) {
            System.out.printf("| %c | %c | %c |%n", chars[0], chars[1], chars[2]);
        }
    }

    /**
     * play at a position
     *
     * @param x x position
     * @param y y position
     */
    public void play(int x, int y) {
        if (x < 0 || x > 3) {
            throw new RuntimeException("position x: " + x + " is out of bound");
        }

        if (y < 0 || y > 3) {
            throw new RuntimeException("Position y: " + y + " is out of bound");
        }

        if (board[x][y] != ' ') {
            throw new RuntimeException("Position (" + x + ", " + y + ") is occupied");
        }

        board[x][y] = isX ? 'X' : 'O';
        isX = !isX;
    }

    public static void main(String[] args) {
        TickTacToe tickTacToe = new TickTacToe();
        tickTacToe.play(1, 1);
        tickTacToe.play(0, 0);
        tickTacToe.play(0, 0);
    }
}
